package com.yst.feign1.config;

import com.yst.feign1.common.ApiResponse;
import feign.Request;
import feign.codec.DecodeException;
import lombok.Getter;
import lombok.NonNull;

@Getter
public class FeignFailResponseException extends DecodeException {

    // 响应结果
    private final Integer code;
    private final String msg;
    private final Object data;
    // 请求参数
    private final String request;

    public FeignFailResponseException(@NonNull ApiResponse httpResult, @NonNull Request request) {
        super(200, httpResult.getMsg(), request);
        code = httpResult.getCode();
        msg = httpResult.getMsg();
        data = httpResult.getResult();
        this.request = request.toString();
    }

    @Override
    public String getMessage() {
        String requestStr = request.replaceAll("\\n+", ">>>");
        return String.format("[feign调用异常, code: %s]-[%s], requeset=[%s], responseData=[%s]", code, msg, requestStr, data);
    }
}
